package model

import (
	jsoniter "github.com/json-iterator/go"
	"time"
)

type WprItem struct {
	ID           uint      `gorm:"primary_key"`
	CreatedAt    time.Time `json:"createdTime"`
	UpdatedAt    time.Time `json:"updatedTime"`

	ParentID     uint      `json:"parentID"`  //工作计划总结表ID
	ProjectID    uint      `json:"projectID"` //项目ID
	Project      Project   `gorm:"ForeignKey:ProjectID"`
	Expected     bool      `json:"expected"`  //计划内/计划外
	Left         bool      `json:"left"`      //前期遗留
	Text         string    `json:"text"`      //工作事项
	StartTime    time.Time `json:"startTime"` //开始时间
	EndTime      time.Time `json:"endTime"`   //结束时间
	OwnerID      uint      `json:"ownerID"`   //责任人ID
	OwnerUser    User      `gorm:"ForeignKey:OwnerID"`
	Done         bool      `json:"done"`       //是否完成
	Output       string    `json:"output"`     //工作输出
	Redmine      string    `json:"redmine"`    //归档任务号
	FinisherID   uint      `json:"finisherID"` //完成人ID
	FinisherUser User      `gorm:"ForeignKey:FinisherID"`
	Quality      string    `json:"quality"`      //质量
	WorkinzHours uint      `json:"workingHours"` //工时
	Remarks      string    `json:"remarks"`      //备注
}

type CustomWprItem struct {
	ID          uint      `json:"id"`
	ParentID    uint      `json:"parentID"`
	ProjectID   uint      `json:"projectID"`
	ProjectName string    `json:"projectName"`
	Expected    bool      `json:"expected"`
	Left        bool      `json:"left"`
	Text        string    `json:"text"`
	StartTime   time.Time `json:"startTime"`
	EndTime     time.Time `json:"endTime"`

	Owner struct {
		ID       uint   `json:"id"`
		UserName string `json:"userName"`
	} `json:"owner"`

	Done    bool   `json:"done"`
	Output  string `json:"output"`
	Redmine string `json:"redmine"`

	Finisher struct {
		ID       uint   `json:"id"`
		UserName string `json:"userName"`
	} `json:"finisher"`

	Quality      string `json:"quality"`
	WorkinzHours uint   `json:"workingHours"`
	Remarks      string `json:"remarks"`
}

type WprItemCopy struct {
	WprItem
}

func (u *WprItemCopy) MarshalJSON() ([]byte, error) {
	a := CustomWprItem{
		ID:           u.ID,
		ParentID:     u.ParentID,
		Expected:     u.Expected,
		Left:         u.Left,
		Text:         u.Text,
		StartTime:    u.StartTime,
		EndTime:      u.EndTime,
		Done:         u.Done,
		Output:       u.Output,
		Redmine:      u.Redmine,
		Quality:      u.Quality,
		WorkinzHours: u.WorkinzHours,
		Remarks:      u.Remarks,
	}
	a.ProjectName = u.Project.Name

	a.Owner.UserName = u.OwnerUser.UserName
	a.Owner.ID = u.OwnerUser.ID

	a.Finisher.UserName = u.FinisherUser.UserName
	a.Finisher.ID = u.FinisherUser.ID

	return jsoniter.ConfigCompatibleWithStandardLibrary.Marshal(a)
}